<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/16
 * Time: 10:25
 */

namespace App\Services\LifeCycle;


class WechatService
{
    public static function appQrCode($scene, $page='pages/lifeCycle/package/userInfo')
    {
        $access_token = self::getAccessToken();

        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";

        $param = [
            'page' => $page,
            'scene' => $scene,
            'env_version' => env('WECHAT_VERSION'), //develop 开发版/ trail 体验版 /release 正式版
            'check_path' => false
        ];

        $code = curlRequest($url, json_encode($param));

        $code_path = UploadService::base64Upload("data:image/jpeg;base64,".base64_encode($code));

        return $code_path;
    }

    public static function getAccessToken()
    {
        $config = config('wechat.mini_program.default');

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$config['app_id']}&secret={$config['secret']}";

        $res = json_decode(curlRequest($url), true);

        return $res['access_token'];
    }
}